using DesktopWidgetApp.Core.Data;
using DesktopWidgetApp.Core.Services;
using DesktopWidgetApp.Shared;
using DesktopWidgetApp.UI.Helpers;
using System;
using System.IO;
using System.Threading.Tasks;

namespace DataSyncTest
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("开始测试本地数据存储和同步机制...");

            try
            {
                // 创建测试用的临时数据库文件
                string dbPath = Path.Combine(Environment.CurrentDirectory, "test.db");
                if (File.Exists(dbPath))
                {
                    File.Delete(dbPath);
                }

                // 初始化状态仓储和服务
                var statusRepository = new SqliteStatusRepository(dbPath);
                var statusService = new StatusService(statusRepository);
                var syncHelper = new StatusSyncHelper("http://localhost:5000");
                var dataSyncManager = new DataSyncManager(statusRepository, syncHelper);

                // 测试用的用户ID
                Guid userId = new Guid("12345678-1234-1234-1234-123456789012");

                // 测试1: 创建并保存状态
                Console.WriteLine("\n测试1: 创建并保存状态");
                var status = await statusService.UpdateUserStatusAsync(
                    userId, 
                    StatusType.Working, 
                    customText: "正在测试", 
                    color: "Green", 
                    isDoNotDisturb: false
                );
                Console.WriteLine($"状态已创建: ID={status.Id}, 类型={status.Type}, 时间={status.StartTime}");

                // 测试2: 获取当前状态
                Console.WriteLine("\n测试2: 获取当前状态");
                var currentStatus = await statusService.GetUserCurrentStatusAsync(userId);
                Console.WriteLine($"当前状态: ID={currentStatus.Id}, 类型={currentStatus.Type}, 免打扰={currentStatus.IsDoNotDisturb}");

                // 测试3: 更新状态
                Console.WriteLine("\n测试3: 更新状态为会议中");
                var updatedStatus = await statusService.UpdateUserStatusAsync(
                    userId, 
                    StatusType.InMeeting, 
                    customText: "在会议中", 
                    color: "Orange", 
                    isDoNotDisturb: true
                );
                Console.WriteLine($"状态已更新: ID={updatedStatus.Id}, 类型={updatedStatus.Type}, 免打扰={updatedStatus.IsDoNotDisturb}");

                // 测试4: 获取状态历史
                Console.WriteLine("\n测试4: 获取状态历史");
                var statusHistory = await statusService.GetUserStatusHistoryAsync(userId);
                Console.WriteLine($"状态历史记录数: {statusHistory.Count()}");
                foreach (var item in statusHistory)
                {
                    Console.WriteLine($"- ID={item.Id}, 类型={item.Type}, 开始={item.StartTime}, 结束={item.EndTime?.ToString() ?? "当前"}");
                }

                // 测试5: 模拟同步
                Console.WriteLine("\n测试5: 模拟数据同步");
                Console.WriteLine("注意：由于没有实际的服务器，此测试将尝试连接服务器但会失败");
                Console.WriteLine("如果配置了实际的服务器API，此测试将成功同步数据");
                bool syncResult = await dataSyncManager.PerformFullSyncAsync(userId);
                Console.WriteLine($"同步结果: {(syncResult ? "成功" : "失败")}");
                Console.WriteLine($"上次同步时间: {dataSyncManager.LastSuccessfulSync}");

                // 测试6: 同步单个状态
                Console.WriteLine("\n测试6: 同步单个状态");
                syncResult = await dataSyncManager.SyncCurrentStatusAsync(userId);
                Console.WriteLine($"同步结果: {(syncResult ? "成功" : "失败")}");

                // 测试7: 测试免打扰功能
                Console.WriteLine("\n测试7: 测试免打扰功能");
                bool isDoNotDisturb = await statusService.IsUserInDoNotDisturbAsync(userId);
                Console.WriteLine($"用户是否处于免打扰状态: {isDoNotDisturb}");

                // 测试8: 结束当前状态
                Console.WriteLine("\n测试8: 结束当前状态");
                await statusService.EndUserCurrentStatusAsync(userId);
                var statusAfterEnd = await statusService.GetUserCurrentStatusAsync(userId);
                Console.WriteLine($"结束当前状态后，是否还有当前状态: {(statusAfterEnd == null ? "没有" : "有")}");

                // 清理测试文件
                // File.Delete(dbPath);

                Console.WriteLine("\n测试完成！");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"\n测试出错: {ex.Message}");
                Console.WriteLine(ex.StackTrace);
            }

            Console.WriteLine("\n按任意键退出...");
            Console.ReadKey();
        }
    }
}